package pl.com.hme.kody.pocztowe.ui;

import pl.com.hme.kody.pocztowe.R;
import pl.com.hme.kody.pocztowe.service.CodeQueryService;
import pl.com.hme.kody.pocztowe.util.SoftInputTool;
import pl.com.hme.kody.pocztowe.util.UIUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainFragment extends Fragment {

	private static final String TAG = "MainFragment";
	private EditText mEditTextLocalityName;
	private EditText mEditTextStreetName;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		final View root = inflater.inflate(R.layout.fragment_main, container, false);

		mEditTextLocalityName = (EditText) root.findViewById(R.id.EditTextLocalityName);
		mEditTextStreetName = (EditText) root.findViewById(R.id.EditTextStreetName);

		mEditTextLocalityName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					showKeyboard(mEditTextLocalityName);
				}
			}
		});
		
		mEditTextLocalityName.requestFocus();

		SoftInputTool softInputTool = new SoftInputTool((InputMethodManager) getActivity().getSystemService(
				Context.INPUT_METHOD_SERVICE), mEditTextStreetName);
		softInputTool.makeTextViewPerformClickOnSubmit((TextView) mEditTextStreetName, root
				.findViewById(R.id.ButtonSearch));
		
		Button searchButton = (Button) root.findViewById(R.id.ButtonSearch);
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isFormValid()) {
					if (UIUtils.isHoneycombTablet(getActivity())) {
						//TODO: Navigate to tablet friendly results page
					}else{
						Intent intent = new Intent(getActivity(), ResultsActivity.class);
						intent.putExtra(CodeQueryService.EXTRA_QUERY_CITY, mEditTextLocalityName.getText().toString());
						intent.putExtra(CodeQueryService.EXTRA_QUERY_STREET, mEditTextStreetName.getText().toString());
						startActivity(intent);
					}
				} else {
					Toast.makeText(getActivity(), getString(R.string.invalid_data_in_form), Toast.LENGTH_LONG).show();
				}
			}
		});

		return root;
	}


	@Override
	public void onResume() {
		Log.d(TAG, "onResume and visible: "+ isVisible());
		super.onResume();
	}


	@Override
	public void onStart() {
		Log.d(TAG, "onStart and visible: "+ isVisible());
		super.onStart();
		

	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		Log.d(TAG, "onActivityCreated and visible: "+ isVisible());
	}


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		Log.d(TAG, "onActivityResult and visible: "+ isVisible());
	}


	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		Log.d(TAG, "onAttach and visible: "+ isVisible());
	}


	@Override
	public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
		super.onInflate(activity, attrs, savedInstanceState);
		
		Log.d(TAG, "onInflate and visible: "+ isVisible());
	}


	private boolean isFormValid() {
		if (mEditTextLocalityName.getText().length() > 0 && mEditTextStreetName.getText().length() > 0) {
			return true;
		}

		return false;
	}
	
	private void showKeyboard(EditText inputEditText){
		InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		boolean result = imm.showSoftInput(inputEditText, InputMethodManager.SHOW_FORCED);
		
	}
}
